﻿using System;
using System.Web;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Config;
using Framework.Library.Core.Utils;
using Framework.Library.Core.Utils.Security;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Admin.Components.Users
{
    public partial class CreateUser : BaseControl
    {
        private readonly string MENUNAME = string.Empty;

        public CreateUser()
        {
        }

        public CreateUser(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session.GetUser().ISSUPERADMIN > 0)
                pnlSuperadmin.Visible = true;
            if (!Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.QueryString["uid"]))
                {
                    btnUpdate.CommandName = "update";
                    btnUpdate.Text = Localization.GetText("ACTION", "UPDATE");
                    DataBinds();
                }
                TimeZones.DataSource = StaticData.TimeZones;
                TimeZones.DataBind();
                Suspended.DataSource = StaticData.Suspended;
                Suspended.DataBind();
                lblCommandName.Text = MENUNAME;
            }
        }

        private void DataBinds()
        {
            int userId = Convert.ToInt32(Request.QueryString["uid"]);
            PortalUser user = ManagerFactory.PortalUserManager.GetUser(userId);
            if (user != null)
            {
                txtUsername.Text = user.USERNAME;
                txtName.Text = user.NAME;
                hddCurPassword.Value = user.PASSWORD;
                txtDisplayName.Text = user.DISPLAYNAME;
                txtEmail.Text = user.EMAIL;
                txtTel.Text = user.TEL;
                txtMobile.Text = user.MOBILE;
                txtAddress.Text = user.ADDRESS;
                chkIsSuperAdmin.Checked = Convert.ToBoolean(user.ISSUPERADMIN);
                TimeZones.SelectedValue = Convert.ToString(user.TIMEZONE);
                chkIsApproval.Checked = Convert.ToBoolean(user.ISAPPROVED);
                PMNotification.Checked = Convert.ToBoolean(user.PMNOTIFICATION);
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (txtPassword.Text.Trim() != txtPasswordConfirm.Text.Trim()) return;
                var user = new PortalUser();
                user.USERNAME = txtUsername.Text.Trim();
                user.NAME = txtName.Text;
                user.DISPLAYNAME = txtDisplayName.Text.Trim();
                user.EMAIL = txtEmail.Text;
                user.TEL = txtTel.Text.Trim();
                user.MOBILE = txtMobile.Text.Trim();
                user.ADDRESS = txtAddress.Text.Trim();
                if (pnlSuperadmin.Visible)
                    user.ISSUPERADMIN = Convert.ToInt32(chkIsSuperAdmin.Checked);
                else
                    user.ISSUPERADMIN = 0;
                user.JOINED = DateTime.Now;
                user.LASTVISIT = DateTime.Now;
                user.IP = HttpContext.Current.Request.UserHostAddress;
                user.TIMEZONE = Convert.ToInt32(TimeZones.SelectedValue);
                user.ISAPPROVED = Convert.ToInt32(chkIsApproval.Checked);
                var tmpSuspend = new TimeSpan(Convert.ToInt32(Suspended.SelectedValue), 0, 0, 0);
                DateTime dtmSuspend = DateTime.Now + tmpSuspend;
                user.SUSPENDED = dtmSuspend;
                user.PMNOTIFICATION = Convert.ToInt32(PMNotification.Checked);
                if (btnUpdate.CommandName != "update")
                {
                    user.PASSWORD = Crypto.MD5Encrypt(txtPassword.Text.Trim());
                    ManagerFactory.DAOEntity.Insert(user);
                }
                else
                {
                    if (txtPassword.Text == "" || txtPassword.Text == "password")
                        user.PASSWORD = hddCurPassword.Value;
                    else
                        user.PASSWORD = Crypto.MD5Encrypt(txtPassword.Text.Trim());
                    user.USERID = Convert.ToInt32(Request.QueryString["uid"]);
                    ManagerFactory.DAOEntity.Update(user);
                }
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30");
        }
    }
}